<?php

/**
 * 管理员控制器
 */
class AdminController extends \Our\Controller_AbstractAdmin
{
    /**
     * 列表页
     */
    public function indexAction()
    {
        $page = $this->getRequest()->getParam('page', 1);
        $count = $this->getRequest()->getParam('count', 12);

        $where = $this->_getWhere();


        $AdminModel = \Admin\AdminModel::getInstance();
        list($result, $paginator) = $AdminModel->getList([
            'page' => $page,
            'count' => $count,
            'where' => $where,
            'baseUrl' => $this->_getBaseUrl()
        ]);

        $this->getView()->assign('paginator', $paginator);
        $this->getView()->assign('listData', $result);
    }

    /**
     * 添加管理员
     */
    public function addAction()
    {
        $form = new \Admin\AdminAddFormModel();
        if ($this->_isPost()) {
            $form->setData($this->_getPost());
        }

        if ($this->_isPost() && $form->validate()) {
            try {
                $params = $form->getFieldValue();
                $AdminModel = \Admin\AdminModel::getInstance();
                $AdminModel->add($params);
                return $this->redirect("/admin/admin/index/");
            } catch (\Error\OurExceptionModel $e) {
                $this->_assignErrorMessage($e->getMessage(), $e->getCode());
            }
        }

        $AdminDAOModel = \Admin\AdminDAOModel::getInstance();

        $this->_assign('statusConfig', $AdminDAOModel->statusConfig);
        $this->_assign('form', $form);
    }

    /**
     * 编辑信息
     */
    public function editAction()
    {
        $admin = $this->_validateAdmin($this->_getQuery("admin_id"));
        $form = new \Admin\AdminAddFormModel();
        //编辑管理员密码非必填
        $form->setFiledsAttr("admin_password", array("require" => false));
        if ($this->_isGet()) {
            $form->setData($admin);
            $form->setFiledsAttr("admin_password", array("value" => ""));
        }
        if ($this->_isPost()) {
            $form->setData($this->_getPost());
        }

        if ($this->_isPost() && $form->validate()) {
            try {
                $params = $form->getFieldValue();
                $params['admin_id'] = $admin['admin_id'];

                $AdminModel = \Admin\AdminModel::getInstance();
                $AdminModel->edit($params);
                return $this->redirect("/admin/admin/index/");
            } catch (\Error\OurExceptionModel $e) {
                $this->_assignErrorMessage($e->getMessage(), $e->getCode());
            }
        }

        $AdminDAOModel = \Admin\AdminDAOModel::getInstance();

        $this->_assign('statusConfig', $AdminDAOModel->statusConfig);
        $this->_assign('admin', $admin);
        $this->_assign('form', $form);
        $this->_display('admin/add.phtml');
    }

    /**
     * 修改密码
     *
     * todo 重写一下
     */
    public function editpasswordAction()
    {
        $business = \Business\Admin\AdminModel::getInstance();
        $admin = $business->validateLogin();
        if (!$admin) {
            $this->redirect($business::$_loginUrl);
            exit();
        }

        if ($this->getRequest()->isPost()) {
            $form = new \Forms\Admin\AdminEditPasswordModel($this->_getPost());
            if ($form->validate()) {
                $params = $form->getFieldValue();
                $business = \Business\Admin\AdminModel::getInstance();
                $errorStatus = false;
                //判断原始密码是否正确
                if ($business->encryptPassword($params['origin_password']) != $admin['admin_password']) {
                    $formErrorMessages['origin_password'] = "原始密码不正确";
                    $errorStatus = true;
                }

                //判断新密码和确认密码是否一致
                if ($params['new_password'] != $params['repeat_password']) {
                    $formErrorMessages['new_password'] = "新密码和确认密码不一致";
                    $errorStatus = true;
                }
                if ($errorStatus) {
                    $this->getView()->assign('formErrorMessages', $formErrorMessages);
                    $this->display('editpassword');
                    exit();
                }

                //更新新密码
                $updateData = array('admin_password' => $business->encryptPassword($params['new_password']));
                $dao = \DAO\Admin\AdminModel::getInstance();
                if ($dao->update($updateData, array('admin_id=?' => $admin['admin_id']))) {
                    $this->getView()->assign('result', array('status' => 1, 'message' => '修改成功'));
                    $business->logout();
                } else {
                    $this->getView()->assign('result', array('status' => 0, 'message' => '修改失败'));
                }
            } else {
                $this->getView()->assign('result', array('status' => -1, 'message' => '表单验证失败'));
                $this->getView()->assign('formErrorMessages', $form->getMessages());
            }
        }
    }

    /**
     * 校验管理员是否存在
     *
     * @param int $id
     * @return array
     */
    protected function _validateAdmin($id)
    {
        $AdminDAOModel = \Admin\AdminDAOModel::getInstance();
        $model = $AdminDAOModel->find($id);
        if (!$model) {
            exit();
        }
        return $model;
    }

}
